package com.zjxf.controller;

import com.alibaba.fastjson.JSONObject;
import com.zjxf.base.bean.po.Admin;
import com.zjxf.base.bean.po.News;
import com.zjxf.base.bean.ro.RoNews;
import com.zjxf.base.bean.vo.VoNews;
import com.zjxf.base.common.JSONResponse;
import com.zjxf.base.common.OperationException;
import com.zjxf.base.common.SysConst;
import com.zjxf.controller.common.BaseController;
import com.zjxf.service.NewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 新闻控制器
 *
 * @author ZhangWei
 * @date 2021/3/24/024 20:20
 */
@Validated
@RestController
@AllArgsConstructor
@Api(tags = "新闻")
@RequestMapping("/news/")
public class NewsController extends BaseController {

    private final NewsService newsService;

    @ApiOperation(value = "删除")
    @PostMapping("delete")
    public JSONResponse delete(@NotNull(message = "请填写新闻id") @RequestParam Long id) throws OperationException {
        newsService.delete(id);
        return JSONResponse.success(SysConst.SUCCESS);
    }

    @ApiOperation(value = "回显")
    @PostMapping("getEntity")
    public JSONResponse getEntity(@NotNull(message = "请填写新闻id") @RequestParam Long id) throws OperationException {
        News news = newsService.getEntity(id);
        RoNews.entity roNews = newsService.getEntityRo(news);
        return JSONResponse.success(roNews);
    }

    @ApiOperation(value = "分页")
    @PostMapping("page")
    public JSONResponse page(@Valid @RequestBody VoNews.filter filter) {
        Page<News> newsPage = newsService.page(filter);
        JSONObject jsonData = newsService.conversion(newsPage);
        return JSONResponse.success(jsonData);
    }

    @ApiOperation(value = "审核")
    @PostMapping("check")
    public JSONResponse check(@Valid @RequestBody VoNews.check check) throws OperationException {
        Admin admin = (Admin)getUser();
        newsService.check(check, admin);
        return JSONResponse.success(SysConst.SUCCESS);
    }

}
